Write-Host "=== 修复数据库迁移问题 ===" -ForegroundColor Green

Write-Host "`n1. 检查当前迁移状态..." -ForegroundColor Yellow
try {
    dotnet ef migrations list -p BackOffice.Infrastructure -s BackOffice.Api
    Write-Host "✓ 迁移列表获取成功" -ForegroundColor Green
} catch {
    Write-Host "✗ 获取迁移列表失败: $($_.Exception.Message)" -ForegroundColor Red
}

Write-Host "`n2. 重置数据库迁移状态..." -ForegroundColor Yellow
Write-Host "执行以下命令来重置迁移状态:" -ForegroundColor Cyan
Write-Host "dotnet ef database drop -p BackOffice.Infrastructure -s BackOffice.Api --force" -ForegroundColor White
Write-Host "dotnet ef database update -p BackOffice.Infrastructure -s BackOffice.Api" -ForegroundColor White

Write-Host "`n3. 或者手动修复迁移历史..." -ForegroundColor Yellow
Write-Host "在PostgreSQL数据库中执行以下SQL:" -ForegroundColor Cyan
Write-Host @"
-- 删除迁移历史表（如果存在）
DROP TABLE IF EXISTS "__EFMigrationsHistory";

-- 重新创建迁移历史表
CREATE TABLE "__EFMigrationsHistory" (
    "MigrationId" character varying(150) NOT NULL,
    "ProductVersion" character varying(32) NOT NULL,
    CONSTRAINT "PK___EFMigrationsHistory" PRIMARY KEY ("MigrationId")
);

-- 插入最新的迁移记录
INSERT INTO "__EFMigrationsHistory" ("MigrationId", "ProductVersion")
VALUES ('20250801073335_InitialCreate', '8.0.0');
"@ -ForegroundColor White

Write-Host "`n4. 检查数据库中的数据..." -ForegroundColor Yellow
Write-Host "执行以下SQL检查数据:" -ForegroundColor Cyan
Write-Host @"
-- 检查用户数据
SELECT COUNT(*) FROM "AppUsers";

-- 检查角色数据
SELECT COUNT(*) FROM "AppRoles";

-- 检查权限数据
SELECT COUNT(*) FROM "AppPermissions";
"@ -ForegroundColor White

Write-Host "`n5. 如果数据为空，插入测试数据..." -ForegroundColor Yellow
Write-Host "执行 scripts/test-data.sql 中的SQL语句" -ForegroundColor Cyan 